//
//  ReservationViewController.m
//  Park
//
//  Created by 123456 on 2017/12/27.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "ReservationViewController.h"
#import "ReservationTopView.h"
#import "ReservationBottomView.h"
#import "ReservationTableViewCell.h"
#import "DateViewController.h"
#import "RoomDetailsVIew.h"
#import "ReservationModel.h"
#import "ReservationViewModel.h"
#import "ReservationOrderViewController.h"

@interface ReservationViewController ()
{
    
    ReservationTopView *Tview;
    ReservationBottomView *Bview;
    RoomDetailsVIew *Rview;
    NSInteger money;
    

    
}

@end

@implementation ReservationViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    
    [self RequestData];

    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self.ReservationTableview registerNib:[UINib nibWithNibName:@"ReservationTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.ReservationTableview.bounces = NO;
    self.title = @"宿舍";
    Tview = [[NSBundle mainBundle]loadNibNamed:@"ReservationTopView" owner:self options:nil][0];
    Tview.frame = CGRectMake(0,0,self.view.size.width,275);
    self.ReservationTableview.tableHeaderView = Tview;

    Bview = [[NSBundle mainBundle]loadNibNamed:@"ReservationBottomView" owner:self options:nil][0];
    Bview.frame = CGRectMake(0,0,self.view.size.width,315);
    self.ReservationTableview.tableFooterView = Bview;
    
    Rview = [[NSBundle mainBundle]loadNibNamed:@"RoomDetailsVIew" owner:self options:nil][0];
    Rview.frame = CGRectMake(0,100,self.view.bounds.size.width,495);
//    [self.view addSubview:Rview];

    

}



-(void)RequestData{
    

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [formatter dateFromString:Tview.DepartureLabel.text];
    NSDate *yesterday = [NSDate dateWithTimeInterval:-60 * 60 * 24 sinceDate:date];

    NSDictionary *dic = @{@"token":[NSUD objectForKey:@"token"],@"startTime":Tview.CheckLabel.text,@"endTime":[formatter stringFromDate:yesterday]};
    
    NSLog(@"%@",dic);
    
    [ReservationViewModel requestWithUrlForRoom:self andParam:dic success:^(NSMutableArray *model) {
        
        NSLog(@"%@",model);
        
        [self.dataArr removeAllObjects];
        
        for (NSDictionary *dic in model) {
            
            for (NSDictionary *dict in dic[@"roomDp"]) {
                ReservationModel *model  =[ReservationModel mj_objectWithKeyValues:dict];

                [self.dataArr addObject:model];
            }
        
        }
        
        
        [self.ReservationTableview reloadData];
        
        
    } failure:^(NSString *error) {
        [MBProgressHUD showError:error toView:self.view];

    }];
    
    
    
}




#pragma mark -  TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return 90;
}


- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    
    ReservationTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    ReservationModel *model  = self.dataArr[indexPath.row];
    
    cell.TitleLabel.text = model.name;
    cell.PriceLabel.text = [NSString stringWithFormat:@"￥%@",model.price];

    if ([model.roomNumber isEqualToString:@"0"]) {
        [cell.BookBUtton setTitle:@"已满" forState:UIControlStateNormal];
        [cell.BookBUtton setBackgroundColor:RGB(193, 33, 71, 1.0)];
    }else{
        [cell.BookBUtton setTitle:@"预订" forState:UIControlStateNormal];
        [cell.BookBUtton setBackgroundColor:RGB(255, 192, 1, 1.0)];

        
    }
    
    [cell.BookBUtton addTarget:self action:@selector(Book:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

      
    
}

//订房
-(void)Book:(UIButton *)button{
    
    


    
    UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
    // 获取cell的indexPath
    NSIndexPath *indexPath = [self.ReservationTableview indexPathForCell:cell];
    ReservationModel *model  = self.dataArr[indexPath.row];

    
    money = [[NSUD objectForKey:@"money"] integerValue] - model.price.integerValue;

    
    if (money>=0) {
        
        if ([model.roomNumber isEqualToString:@"0"]) {
            
            [MBProgressHUD showError:@"无剩余空房，请选择其它房型！" toView:self.view];
            
        }else{
            
            ReservationOrderViewController *r = [[ReservationOrderViewController alloc]init];
            int i = Tview.Common.intValue *model.price.intValue;
            r.Pricestr = [NSString stringWithFormat:@"%d",i];
            r.Checkstr = Tview.CheckLabel.text;
            r.Departurestr = Tview.DepartureLabel.text;
            r.Commonstr = Tview.Common;
            r.Roomstr = model.name;
            r.RoomNumber =model.roomNumber;
            r.RoomID = model.id;
            
            [self.navigationController pushViewController:r animated:YES];
            
        }
        
    }else{
        
        [MBProgressHUD yty_showErrorWithTitle:nil detailsText:@"账户积分不足支付该订单，请到前台进行充值！"toView:self.view];

        
    }
    

}


-(NSMutableArray *)dataArr{
    
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    
    return _dataArr;
}

@end
